package com.yzt.base.utils;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;
import net.coobird.thumbnailator.util.ThumbnailatorUtils;

import java.io.File;
import java.io.IOException;

public class ThumbnailUtils {

    /**
     * 图片按照宽高压缩 默认 宽高为 300：300
     *
     * @param filePath 图片路径
     * @return
     */
    public static String thumbnail(String filePath) {
        return thumbnail(new File(filePath), 300, 300);
    }

    /**
     * 图片按照宽高压缩 默认 宽高为 300：300
     *
     * @param file 图片文件
     * @return
     */
    public static String thumbnail(File file) {
        return thumbnail(file, 300, 300);
    }

    public static String thumbnail(String filePath, int width, int height) {
        return thumbnail(new File(filePath), width, height);
    }

    public static String thumbnail(File file, int width, int height) {
        File newFile = createThumbFile(file);
        try {
            Thumbnails.of(file).size(width, height).toFile(newFile);
            return newFile.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String rotate(String filePath, int deg) {
        File f = new File(filePath);
        return rotate(f, deg);
    }

    public static String rotate(File file, int deg) {
        try {
            File newFile = createThumbFile(file);
            Thumbnails.of(file).rotate(deg).toFile(newFile);
            return file.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String scale(String filePath, double scale) {
        return scale(new File(filePath), scale);
    }

    public static String scale(File file, double scale) {
        try {
            File newFile = createThumbFile(file);
            Thumbnails.of(file).scale(scale).toFile(newFile);
            return newFile.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static File createThumbFile(File file) {
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.indexOf("."), fileName.length());
        File dir = file.getParentFile();
        File newFile = new File(dir, fileName.replace(suffix, "-thumb" + suffix));
        return newFile;
    }

    public static void main(String[] args) {
        rotate("C:\\Users\\demo\\Desktop\\img\\viewPic.jpg", 90);
    }
}
